package ru.minidoc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

import ru.minidoc.model.Document;

public class DocumentHistoryDao extends AbstractDao {
    public String getText(Document document, Integer version) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        String text = jdbcTemplate.query(
                "select text from documenthistory where documentId = ? and version = ?",
                new Object[] { document.getId(), version },
                new ResultSetExtractor<String>() {

                    @Override
                    public String extractData(ResultSet rs)
                            throws SQLException, DataAccessException {
                        if (rs.next()) {
                            return rs.getString(1);
                        }
                        return "";
                    }
                });
        return text;
    }

    public List<Document> getHistoryList(Document document) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        List<Document> result = jdbcTemplate
                .query("select dh.id, dh.name, dh.assignedTo, dh.modified, dh.projectId,"
                        + " dh.modifiedBy, user.fullname, project.name, dh.version, dh.documentId from documenthistory dh"
                        + " left join user on user.id = dh.assignedTo"
                        + " left join project on project.id = dh.projectId"
                        + " where dh.documentId = ?", new Object[] { document.getId()},
                        new RowMapper<Document>() {

                            @Override
                            public Document mapRow(ResultSet rs, int rowNum)
                                    throws SQLException {
                                Document history = new Document();
                                history.setId(rs.getInt(1));
                                history.setName(rs.getString(2));
                                history.setAssignedToId(rs.getInt(3));
                                history.setModified(new SimpleDateFormat()
                                        .format(rs.getTimestamp(4)));
                                history.setProjectId(rs.getInt(5));
                                history.setModifiedBy(rs.getString(6));
                                history.setAssignedTo(rs.getString(7));
                                history.setProject(rs.getString(8));
                                history.setVersion(rs.getInt(9));
                                history.setDocumentId(rs.getInt(10));
                                return history;
                            }
                        });
        return result;
    }
}